<?php  

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Singers extends Admin
{

    public function __construct(){
    parent::initialize();
    // var_dump($this->initialize()) ;

   }
   public function singers(){
  
     $list = Db::name('tb_singers')->paginate(3);
     $this->assign('list',$list);  
  


     return $this->fetch('singers/singers');
   }

   //歌手编辑第一步
   public function edit($id)

   {


    $list = Db::name('tb_singers')->where('id',$id)->select();
    // var_dump($list);die;
    $data=$list[0];
    $this->assign('data',$data); 

    
    return $this->fetch('singers/update');
   	 
   }

   //歌手编辑第二步
   public function edits(Request $request)
   {
   	 // 获取表单上传文件
        $file = $request->file('image');
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
    // 移动到框架应用根目录/public/uploads/ 目录下
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       $icon=$info->getSaveName();
       $icon = str_replace('\\','/',$icon);


         //接收post数据
        $list = Request::instance()->post();

         //处理数据
        $data=['name'=>$list['name'],
               'address'=>$list['address'],
               'num'=>$list['num'],
               'sex'=>$list['sex'],
               'dex'=>$list['dex'],
               'style'=>$list['style'],
               'icon'=>$icon,
        
              ];


        $result= Db::name('tb_singers')->where("id",$list['id'])->update($data);


          if ($result >0) {
          return $this->success('编辑成功',url('admin/Singers/singers'));
       }else{
          return $this->success('编辑失败');
       }



    
   }


   //歌手删除
   public function delete($id)
   {

   	 $result = Db::name('tb_singers')->delete($id);
     
     if ($result >0 ) {
       $info['status'] = true;
     }else{
       $info['status'] = false;
     }

     return $info;  




   	 return $this->fetch('singers/singers');
   }

   //歌手增加
   public function add(Request $request)
   {   

       

       // 获取表单上传文件
        $file = $request->file('image');
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
    // 移动到框架应用根目录/public/uploads/ 目录下
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       $icon=$info->getSaveName();
       $icon = str_replace('\\','/',$icon);

    
   	  //接收post数据
        $list = Request::instance()->post();
        // var_dump($list);die;
      //处理数据
        $data=['name'=>$list['name'],
               'address'=>$list['address'],
               'num'=>$list['num'],
               'sex'=>$list['sex'],
               'dex'=>$list['dex'],
               'style'=>$list['style'],
               'icon'=>$icon

              ];

        $result= Db::name('tb_singers')->data($data)->insert();

          if ($result >0) {
          return $this->success('添加成功',url('admin/Singers/singers'));
       }else{
          return $this->success('添加失败');
       }



   }
}